﻿using System;
using System.IO;
using Presentation;

namespace Infrastructure
{
    public class HTMLLogger: ILogger
    {
        public string FileName { get; set; }
        public bool CanWriteTimeMarks { get;  set; }

        public HTMLLogger(bool canWriteTimeMarks = true)
        {
            CanWriteTimeMarks = canWriteTimeMarks;
            var creationTime = DateTime.Now.ToString("yyyy-MMM-dd_HH-mm-ss");
            FileName = creationTime + "_" + "log.html";
            var writer = new StreamWriter(FileName, false);
            writer.Close();
        }
        public void WriteLog(string log, bool writeTimeMark = true)
        {
            var time = DateTime.Now.ToString("HH:mm:ss");
            using (var writer = new StreamWriter(FileName, true))
            {
                var line = "";
                if (CanWriteTimeMarks)
                {
                    if (writeTimeMark)
                    {
                        line = "[" + time + "] ";
                    }
                    else
                    {
                        line = "           ";
                    }
                }
                log = log + "<br>";
                line += log;
                writer.WriteLine(line);
                writer.Close();
            }
        }
        public String ReadLog()
        {
            StreamReader logFile = new StreamReader(FileName);
            String log = logFile.ReadToEnd();
            logFile.Close();
            return log;
        }
        public void  DeleteLog()
        {
            StreamWriter logFile = new StreamWriter(FileName, false);
            logFile.Close();
        }
    }
}
